package phase01.module2.code.question1;

import java.util.Arrays;
import java.util.Optional;

public class TwoDimensionArray {
    private final int SIZE = 16;
    private final int[][] twoDArray;

    public TwoDimensionArray() {
        twoDArray = new int[SIZE][SIZE];
        for (int i = 0; i < SIZE; i++) {
            Arrays.fill(twoDArray[i], i + 1);
        }
    }

    public void show() {
        for (int i = 0; i < SIZE; i++) {
            System.out.println(Arrays.toString(twoDArray[i]));
        }
    }

    public int sumTopLeft2BottomRight() {
        int sum = 0;
        for (int i = 0; i < SIZE; i++) {
            sum += twoDArray[i][i];
        }
        return sum;
    }

    public int sumTopRight2BottomLeft() {
        int sum = 0;
        for (int i = SIZE - 1; i >= 0; i--) {
            sum += twoDArray[i][i];
        }
        return sum;
    }

    public int sumAll() {
        Optional<Integer> sum = Arrays.stream(twoDArray).map((oneD) -> Arrays.stream(oneD).sum()).reduce(Integer::sum);
        return sum.orElse(0);
    }
}
